package com.unity3d.player;

import android.app.*;
import android.os.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import android.content.*;
import android.view.*;

final class b
{
    private l a;
    private boolean b;
    private boolean c;
    private boolean d;
    private Context e;
    private d f;
    private String g;

    public b(final d f) {
        this.a = null;
        this.b = false;
        this.c = false;
        this.d = false;
        this.e = null;
        this.f = null;
        this.g = "";
        this.f = f;
    }

    private void a(final Runnable runnable) {
        final Context e = this.e;
        if (e instanceof Activity) {
            ((Activity)e).runOnUiThread(runnable);
            return;
        }
        com.unity3d.player.e.Log(5, "Not running Google VR from an Activity; Ignoring execution request...");
    }

    private void a(final String s) {
        final d f = this.f;
        if (f != null) {
            final StringBuilder sb = new StringBuilder("Google VR Error [");
            sb.append(this.g);
            sb.append("]");
            f.reportError(sb.toString(), s);
            return;
        }
        final StringBuilder sb2 = new StringBuilder("Google VR Error [");
        sb2.append(this.g);
        sb2.append("]: ");
        sb2.append(s);
        com.unity3d.player.e.Log(6, sb2.toString());
    }

    private static boolean a(final int n) {
        return Build.VERSION.SDK_INT >= n;
    }

    private boolean a(final ClassLoader classLoader) {
        try {
            final Class<?> loadClass = classLoader.loadClass("com.unity3d.unitygvr.GoogleVR");
            (this.a = new l(loadClass, loadClass.getConstructor((Class<?>[])new Class[0]).newInstance(new Object[0]))).a("initialize", new Class[] { Activity.class, Context.class, SurfaceView.class, Boolean.TYPE });
            this.a.a("deinitialize", new Class[0]);
            this.a.a("load", new Class[] { Boolean.TYPE, Boolean.TYPE, Boolean.TYPE, Runnable.class });
            this.a.a("enable", new Class[] { Boolean.TYPE });
            this.a.a("unload", new Class[0]);
            this.a.a("pause", new Class[0]);
            this.a.a("resume", new Class[0]);
            this.a.a("getGvrLayout", new Class[0]);
            return true;
        }
        catch (Exception ex) {
            final StringBuilder sb = new StringBuilder("Exception initializing GoogleVR from Unity library. ");
            sb.append(ex.getLocalizedMessage());
            this.a(sb.toString());
            return false;
        }
    }

    private void b(final boolean c) {
        this.c = c;
    }

    private boolean b(final Runnable runnable) {
        final Semaphore semaphore = new Semaphore(0);
        this.a(new Runnable() {
            @Override
            public final void run() {
                try {
                    try {
                        runnable.run();
                        semaphore.release();
                    }
                    finally {
                        semaphore.release();
                        while (true) {}
                    }
                }
                catch (Exception ex) {}
            }
        });
        try {
            if (!semaphore.tryAcquire(4L, TimeUnit.SECONDS)) {
                this.a("Timeout waiting for vr state change!");
                return false;
            }
            return true;
        }
        catch (InterruptedException ex) {
            final StringBuilder sb = new StringBuilder("Interrupted while trying to acquire sync lock. ");
            sb.append(ex.getLocalizedMessage());
            this.a(sb.toString());
            return false;
        }
    }

    private boolean e() {
        return this.c;
    }

    public final long a(final boolean b, final boolean b2, final boolean b3, final Runnable runnable) {
        final AtomicLong atomicLong = new AtomicLong(0L);
        String g;
        if (!b && !b2) {
            g = "Cardboard";
        }
        else {
            g = "Daydream";
        }
        this.g = g;
        if (!this.b(new Runnable() {
            @Override
            public final void run() {
                try {
                    atomicLong.set((long)com.unity3d.player.b.this.a.a("load", b, b2, b3, runnable));
                    com.unity3d.player.b.this.c();
                }
                catch (Exception ex) {
                    final b f = com.unity3d.player.b.this;
                    final StringBuilder sb = new StringBuilder("Exception caught while loading GoogleVR. ");
                    sb.append(ex.getLocalizedMessage());
                    f.a(sb.toString());
                    atomicLong.set(0L);
                }
            }
        }) || atomicLong.longValue() == 0L) {
            this.a("Google VR had a fatal issue while loading. VR will not be available.");
        }
        return atomicLong.longValue();
    }

    public final void a() {
        final Activity activity = (Activity)this.e;
        if (this.d && !this.b && activity != null) {
            this.b = true;
            final Intent intent = new Intent("android.intent.action.MAIN");
            intent.addCategory("android.intent.category.HOME");
            intent.setFlags(268435456);
            activity.startActivity(intent);
        }
    }

    public final void a(final Intent intent) {
        if (intent != null && intent.getBooleanExtra("android.intent.extra.VR_LAUNCH", false)) {
            this.d = true;
        }
    }

    public final void a(final boolean b) {
        if (this.f != null) {
            if (this.e == null) {
                return;
            }
            this.a(new Runnable() {
                @Override
                public final void run() {
                    if (b == com.unity3d.player.b.this.e()) {
                        return;
                    }
                    try {
                        if (!b || com.unity3d.player.b.this.e()) {
                            if (!b && com.unity3d.player.b.this.e()) {
                                com.unity3d.player.b.this.b(false);
                                if (com.unity3d.player.b.this.a != null) {
                                    com.unity3d.player.b.this.a.a("enable", false);
                                }
                                if (com.unity3d.player.b.this.a != null && com.unity3d.player.b.this.f != null) {
                                    com.unity3d.player.b.this.f.removeViewFromPlayer((View)com.unity3d.player.b.this.a.a("getGvrLayout", new Object[0]));
                                }
                            }
                            return;
                        }
                        if (com.unity3d.player.b.this.a != null && com.unity3d.player.b.this.f != null && !com.unity3d.player.b.this.f.addViewToPlayer((View)com.unity3d.player.b.this.a.a("getGvrLayout", new Object[0]), true)) {
                            com.unity3d.player.b.this.a("Unable to add Google VR to view hierarchy.");
                            return;
                        }
                        if (com.unity3d.player.b.this.a != null) {
                            com.unity3d.player.b.this.a.a("enable", true);
                        }
                        com.unity3d.player.b.this.b(true);
                    }
                    catch (Exception ex) {
                        final b b = com.unity3d.player.b.this;
                        final StringBuilder sb = new StringBuilder("Exception enablinb Google VR on UI Thread. ");
                        sb.append(ex.getLocalizedMessage());
                        b.a(sb.toString());
                    }
                }
            });
        }
    }

    public final boolean a(final Activity activity, final Context e, final SurfaceView surfaceView) {
        String s;
        if (activity != null && e != null && surfaceView != null) {
            this.e = e;
            if (!a(19)) {
                s = "Google VR requires a device that supports an api version of 19 (KitKat) or better.";
            }
            else if (this.d && !a(24)) {
                s = "Daydream requires a device that supports an api version of 24 (Nougat) or better.";
            }
            else {
                if (!this.a(UnityPlayer.class.getClassLoader())) {
                    return false;
                }
                boolean booleanValue;
                try {
                    booleanValue = (boolean)this.a.a("initialize", activity, e, surfaceView, this.d);
                }
                catch (Exception ex) {
                    final StringBuilder sb = new StringBuilder("Exception while trying to intialize Unity Google VR Library. ");
                    sb.append(ex.getLocalizedMessage());
                    this.a(sb.toString());
                    booleanValue = false;
                }
                if (!booleanValue) {
                    this.a("Unable to initialize GoogleVR library.");
                }
                this.b(false);
                this.b = false;
                this.g = "";
                return true;
            }
        }
        else {
            s = "Invalid parameters passed to Google VR initiialization.";
        }
        this.a(s);
        return false;
    }

    public final void b() {
        final l a = this.a;
        if (a != null) {
            a.a("pause", new Object[0]);
        }
    }

    public final void c() {
        final l a = this.a;
        if (a != null) {
            a.a("resume", new Object[0]);
        }
    }

    public final void d() {
        this.a(false);
        this.a(new Runnable() {
            @Override
            public final void run() {
                try {
                    if (com.unity3d.player.b.this.a != null) {
                        com.unity3d.player.b.this.a.a("unload", new Object[0]);
                        com.unity3d.player.b.this.a.a("deinitialize", new Object[0]);
                        com.unity3d.player.b.this.a = null;
                    }
                    com.unity3d.player.b.this.b(false);
                }
                catch (Exception ex) {
                    final b a = com.unity3d.player.b.this;
                    final StringBuilder sb = new StringBuilder("Exception unloading Google VR on UI Thread. ");
                    sb.append(ex.getLocalizedMessage());
                    a.a(sb.toString());
                }
            }
        });
    }
}
